
let isLock = false;
let lockList = [];

module.exports = {
    
    //代码锁，并发锁
    async lock() {
        function unlock() {
            //拿出第一个promoise的可调用对象，如果有则结束调这个并继续执行
            let waitFunc = lockList.shift();
            if (waitFunc) {
                waitFunc.resolve(unlock);
            } else {
                isLock = false;
            }
        }
        if (isLock) {
            //已经锁定时再次执行创建一个promise到locklist
            return new Promise((resolve, reject) => {
                lockList.push({ resolve, reject });
            });
        } else {
            //第一次执行继续执行
            isLock = true;
            return unlock;
        }
    },
    now2str() {
        var d = new Date()
        return d.getFullYear() + "-" + (d.getMonth() + 1 < 10 ? "0" : "") + (d.getMonth() + 1) + "-" +
            (d.getDate() < 10 ? "0" : "") + d.getDate() + " " +
            (d.getHours() < 10 ? "0" : "") + d.getHours() + ":" +
            (d.getMinutes() < 10 ? "0" : "") + d.getMinutes() + ":" +
            (d.getSeconds() < 10 ? "0" : "") + d.getSeconds();
    },
    //反转safeid（分表之后查询用）
    safeid2id(said) {
        var x = "0qwertlz678gxc34vbnm12ayuiopdfs5hjk9"
        var said_index = []
        for (var i = 0; i < said.length; i++){
            if (x.indexOf(said[i]) == -1) {
                return null
            } else {
                said_index.push(x.indexOf(said[i]))
            }
        }
        if (said.length <= 2) {
            return ((said_index[0]<<5)+said_index[1] & 0xff) << 0
        }
        if (said.length <= 4) {
            var b1 = ((said_index[0] << 5) + said_index[1] & 0xff) << 0
            var b2 = ((said_index[2] << 5) + said_index[3] & 0xff) << 8
            return b1+b2
        }
        if (said.length <= 6) {
            var b1 = ((said_index[0] << 5) + said_index[1] & 0xff) << 0
            var b2 = ((said_index[2] << 5) + said_index[3] & 0xff) << 8
            var b3 = ((said_index[4] << 5) + said_index[5] & 0xff) << 16
            return b1+b2+b3
        }
        if (said.length <= 8) {
            var b1 = ((said_index[0] << 5) + said_index[1] & 0xff) << 0
            var b2 = ((said_index[2] << 5) + said_index[3] & 0xff) << 8
            var b3 = ((said_index[4] << 5) + said_index[5] & 0xff) << 16
            var b4 = ((said_index[6] << 5) + said_index[7] & 0xff) << 16
            return b1+b2+b3+b4
        }
    },
    //最大支持数为 21474836473,将ID转换成无序加密字符串
    id2safeid(v) {
        var x = "0qwertlz678gxc34vbnm12ayuiopdfs5hjk9"

        var b1 = v & 0xff
        var b2 = v >> 8 & 0xff
        var b3 = v >> 16 & 0xff
        var b4 = v >> 24 & 0xff

        //var x1 = (b1 &0xff) <<0
        //var x2 = (b2 &0xff) <<8
        //var x3 = (b3 & 0xff) << 16
        //var x4 = (b4 & 0xff) << 24


        function getb4(k) {
            var x = k >> 5 << 5
            if (x == 0) {
                return [0, k]
            } else {
                return [k >> 5, k - x]
            }
        }
        var c1 = getb4(b1)
        var c2 = getb4(b2)
        var c3 = getb4(b3)
        var c4 = getb4(b4)

        var p = x[c1[0]] +
            x[c1[1]] +
            "," +
            x[c2[0]] +
            x[c2[1]] +
            "," +
            x[c3[0]] +
            x[c3[1]] +
            "," +
            x[c4[0]] +
            x[c4[1]];
        return p.split(",00").join("").split(",").join("");
    }

}